#include<stdio.h>
#include "serialprotocol.h"
#include "irfid.h"

void test(){
    u8 info[]={0x30,0x31,0x32};
    TranData *data=initTranData(0x00,0x12,12,info,sizeof(info));
    u8 *serial=serialize(data);
    TranData *deserial=deserialize(serial);
    showTranData(deserial);
    free(data);
    free(serial);
    free(deserial);
}

int feed_comm(TranData *data)
{
    u8 status=1;
    switch(data->opt)
    {
        case READ:
            printf("Read\n");
            status=Read_card(15);
            if(status!=STATUS_OK){
                printf("Read error!\n");
            }
            break;
        case WRITE:
            printf("Write\n");
            status=Write_card(data->addr,data->data);
            if(status!=STATUS_OK){
                printf("Read error!\n");
            }
            break;
        case READ_ID:
            printf("Read ID\n");
            status=Read_ID();
            break;
        case AUTH_KEYA:
            printf("AUTH_KEYA\n");
            status=Auth_block(KEY_A,data->addr,data->data);
            if(status!=STATUS_OK){
                printf("AUTH error!\n");
            }
            break;
        case AUTH_KEYB:
            printf("AUTH_KEYB\n");
            status=Auth_block(KEY_B,data->addr,data->data);
            if(status!=STATUS_OK){
                printf("AUTH error!\n");
            }
            break;
        default:
            printf("UnKnown\n");
            break;
    }
    return status;
}
int main_workers()
{
    u8 info[]={0x30,0x31,0x32,0x30,0x31,0x32};
    TranData *data1=initTranData(0x01,READ_ID,3,info,0);
    TranData *data2=initTranData(0x01,WRITE,3,info,6);
    u8 status=1;

    status=feed_comm(data1);
    if(status==STATUS_OK)
        printf("操作成功\n\n");
    else
        printf("操作失败\n\n");
    status=feed_comm(data2);
    if(status==STATUS_OK)
        printf("操作成功\n\n");
    else
        printf("操作失败\n\n");
}
int main()
{
    //test();
    main_workers();
    return 0;
}
